package com.carltechnology.mahjonggamecaculation.dao;

import com.carltechnology.mahjonggamecaculation.base.MyApplication;
import com.carltechnology.mahjonggamecaculation.dao.impl.MahjongGameDao;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class DataManager {
    // do function with component of datasourcecomponent
    private final MahjongDataSourceHelper dataSourceHelper;
    // do function with component of insertdatacomponent.
    private final MahjongInsertHelper insertHelper;
    // do function with component of modifycomponent
    private final MahjongModifyHelper modifyHelper;
    private final IMahjongDataDao mDao;
    private static DataManager instance;
    private DataManager() {
        mDao = new MahjongGameDao(MyApplication.getContext());
        dataSourceHelper = new MahjongDataSourceHelper(mDao);
        insertHelper = new MahjongInsertHelper(mDao);
        modifyHelper = new MahjongModifyHelper(mDao);

    }

    public MahjongModifyHelper getModifyHelper() {
        if (!mDao.isOpen()){
            mDao.openDataBase();
        }
        return modifyHelper;
    }

    public MahjongInsertHelper getInsertHelper() {
        if (!mDao.isOpen()){
            mDao.openDataBase();
        }
        return insertHelper;
    }

    public MahjongDataSourceHelper getDataSourceHelper() {
        if (!mDao.isOpen()){
            mDao.openDataBase();
        }
        return dataSourceHelper;
    }

    public static DataManager getInstance(){
        if (instance==null){
            instance = new DataManager();
        }
        return instance;
    }

    // close the database visiting.
    public void closeDataBase(){
        mDao.closeDataBase();
    }
}
